package com.wordpress.minhnvt1;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity {
	
	private View view;
	
	
	private static final int RE_PLAY = 1;
	
	private static final int EXIT = 2;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        LayoutInflater factory = LayoutInflater.from(this);
        
        //no title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        //Full screen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        				WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        //Set game layout
        view = factory.inflate(R.layout.main, null);
        
        setContentView(view);
        view.setFocusable(true);
		view.setFocusableInTouchMode(true);
    }
    
    @Override
    protected void onStop() {
    	super.onStop();
    	((ArcadeGame)view).halt();
    }
    
    @Override
    protected void onPause() {
    	super.onPause();
    	onStop();
    }
    
    @Override
    protected void onRestart() {
    	super.onRestart();
    	((ArcadeGame)view).resume();
    }
    
    @Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_BACK){
			
			AlertDialog.Builder builder= new AlertDialog.Builder(MainActivity.this);
			builder.setMessage("Bạn muốn thoát khỏi trò chơi??");
			builder.setTitle("Chú ý");
			builder.setPositiveButton("OK", new OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
				finish(); 
					
				}
			});
			builder.show();
			
			return true;
		}	
		return false;
	}
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, RE_PLAY, 0, "Chơi Lại");
        menu.add(0, EXIT, 0, "Thoát");      
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case RE_PLAY:
            	
            	((OAnQuanGame)view).initialize();
                return true;
            case EXIT:
            	AlertDialog.Builder builder= new AlertDialog.Builder(MainActivity.this);
    			builder.setMessage("Bạn muốn thoát khỏi trò chơi??");
    			builder.setTitle("Chú ý");
    			builder.setIcon(android.R.drawable.ic_dialog_info);
    			builder.setPositiveButton("Ok", new OnClickListener() {
    				public void onClick(DialogInterface dialog, int which) {
    					finish(); 					
    				}
    			});
    			builder.show();
                return true;
            
        }

        return false;
    }
}